# File: buttonController
# By: Camiel Verschoor, Sander Nugteren & Erik van Egmond

from naoqi import ALProxy
import time

class buttonController():
    # VARIABLES
    listening = False
    memProxy = ALProxy("ALMemory","127.0.0.1", 9559)
    ttsProxy = ALProxy("ALTextToSpeech", "127.0.0.1", 9559)
    chestButtonPressed = False
    manual = True

    # CONSTRUCTOR
    def __init__(self, interval=0.5):
        self.interval = interval
        #self.chestButtonPressed = False
        self.manual = True

    # FUNCTIONS
    def chestButton(self):
        if (self.memProxy.getData("ChestButtonPressed", 0) == 0.0):
            return False
        return True

    def leftBumperButton(self):
        if (self.memProxy.getData("LeftBumperPressed", 0) == 0.0):
            return False
        return True

    '''
    def refresh(self):
        #Keep listening for some time
        if self.getChestButton():
            self.chestButtonPressed = True

    def reset(self):
        self.chestButtonPressed = False
    '''        
    
    # THE FUNCTIONS TO SETUP THE NAO
    def setup(self, color, kickOff):
        self.ttsProxy.say("This is the manual setup. Choose my team")
        if (color == 0):
            self.ttsProxy.say("My current team color is, blue")
        else:
            self.ttsProxy.say("My current team color is, red")
        color = self.chooseTeam(color)
        time.sleep(1)
        self.ttsProxy.say("Choose mode")
        if (kickOff == 0):
            self.ttsProxy.say("Current mode: Match mode")
        else:
            self.ttsProxy.say("Current mode: Penalty mode")
        kickOff = self.chooseKickOff(kickOff)
        time.sleep(1)
        self.manual = False
        return (color, kickOff)

    def chooseTeam(self, color):
        startTime = time.time()
        while (time.time() - startTime < 4):
            if (self.leftBumperButton()):
                if(color == 0):
                    color = 1
                    self.ttsProxy.say("My team color is, red")
                else:
                    color = 0
                    self.ttsProxy.say("My team color is, blue")
                time.sleep(0.5)
        return color

    def chooseKickOff(self, kickOff):
        startTime = time.time()
        while (time.time() - startTime < 4):
            if (self.leftBumperButton()):
                if(kickOff == 0):
                    kickOff = 1
                    self.ttsProxy.say("Penalty mode")
                else:
                    kickOff = 0
                    self.ttsProxy.say("Match mode")
                time.sleep(0.5)
        return kickOff

    # GET FUNCTIONS
    def getChestButton(self):
        button = self.chestButton()
        if button:
            while self.chestButton():
                pass
        return button

    def getSetup(self):
        return self.manual

    # SET FUNCTIONS
    def setListening(self, set):
        self.listening = set